//
//  SchedulersViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/19.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift

class SchedulersViewController: UIViewController {

    private let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "18.调度器、subscribeOn、observeOn"
        view.backgroundColor = .white
    }
    
    // gcd
    func testGCD() {
        //现在后台获取数据
        DispatchQueue.global(qos: .userInitiated).async {
            let data = try? Data(contentsOf: URL(string: "url")! )
            //再到主线程显示结果
            DispatchQueue.main.async {
                print(data as Any)
            }
        }
    }
    
    // scheduler
    func testScheduler() {
        let rxData: Observable<Data> = Observable.of(Data())
         
        rxData
            .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .userInitiated)) //后台构建序列
            .observeOn(MainScheduler.instance)  //主线程监听并处理序列结果
            .subscribe(onNext: { [weak self] data in
                print(data as Any)
            })
            .disposed(by: disposeBag)
    }

}
